<?php

require("./mpcamera/setup.php");

$smarty = new Smarty_MPCamera();

$action = filter_input(INPUT_GET, "action");
$submit = strtolower(filter_input(INPUT_POST, "submit"));

if (1 == 1) {
    // Select ThanhPho
    $query_thanhpho = "select * from thanhpho";
    $result_thanhpho = mysql_query($query_thanhpho);
    while ($row = mysql_fetch_array($result_thanhpho)) {
        $thanhpho[] = $row;
    }
    $smarty->assign("thanhpho", $thanhpho);
    // End
    // Select QuanHuyen
    $query_quanhuyen = "
        SELECT `thanhpho`.`MaTP`, `TenTP`, `quanhuyen`.`MaQH`, `TenQH`
        FROM `thanhpho`, `quanhuyen`, `thanhpho_quanhuyen`
        WHERE `thanhpho`.`MaTP`=`thanhpho_quanhuyen`.`MaTP`  AND `quanhuyen`.`MaQH` = `thanhpho_quanhuyen`.`MaQH`
        ;
    ";
    $result_quanhuyen = mysql_query($query_quanhuyen);
    while ($row = mysql_fetch_array($result_quanhuyen)) {
        $quanhuyen[] = $row;
    }
    $smarty->assign("quanhuyen", $quanhuyen);
    // End
}

if ($action == "add") {
    // Nothing
}

if ($submit == "add") {
    $success = 0;

    $thanhpho = filter_input(INPUT_POST, "thanhpho");
    $quanhuyen = filter_input(INPUT_POST, "quanhuyen");
    $phuongxa = filter_input(INPUT_POST, "phuongxa");
    $address = filter_input(INPUT_POST, "address");

    $query = "
        SELECT *
        FROM `diachi`
        WHERE `DiaChi`='$address' AND 
            `MaPX`=$phuongxa AND
            `MaQH`=$quanhuyen AND
            `MaTP`=$thanhpho
        ;
    ";
    $result = mysql_query($query);
    if (mysql_num_rows($result) != 0) {
        $success = -1;
    } else {
        $query = "
            INSERT INTO `diachi`(`DiaChi`,`MaPX`,`MaQH`, `MaTP`)
            VALUES('" .
                $address . "',
                $phuongxa,
                $quanhuyen,
                $thanhpho
            );
        ";
        $result = mysql_query($query);
        if ($result) {
            $success = 1;
        } else {
            $success = 0;
        }
    }
    $smarty->assign("success", $success);

    // Gọi hàm này thông qua ajax
    $ajax = filter_input(INPUT_POST, "ajax");

    if (isset($success) && isset($ajax)) {
        $query_maDC = "select `MaDC` from `diachi` where `DiaChi`= '$address' AND `MaPX`=$phuongxa AND `MaQH`=$quanhuyen AND `MaTP`=$thanhpho;";
        $result_maDC = mysql_query($query_maDC);
        $_maDC = mysql_fetch_array($result_maDC);
        $maDC = $_maDC['MaDC'];
        $data = array(
            "success" => $success,
            "maDC" => $maDC,
        );
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        die();
    }
    // End
}

if ($action == "view") {
    $query = "
        SELECT `MaDC`, `DiaChi`, `TenPX`, `TenQH`, `TenTP`
        FROM `diachi`, `phuongxa`, `quanhuyen`, `thanhpho`
        WHERE  `diachi`.`MaPX` = `phuongxa`.`MaPX` 
            AND `diachi`.`MaQH` = `quanhuyen`.`MaQH`
            AND `diachi`.`MaTP` = `thanhpho`.`MaTP`
        ORDER BY `TenTP`, `TenQH`, `TenPX`, `DiaChi`
        ;
    ";
    $result = mysql_query($query);
    while ($row = mysql_fetch_array($result)) {
        $diachi[] = $row;
    }
    $smarty->assign("diachi", $diachi);
}

if ($action == "edit") {
    $found = 0;
    $_id = filter_input(INPUT_GET, "id");
    $id = $smarty->clean($_id, true);
    if ($id > 0) {
        $query = "
            SELECT *
            FROM `diachi`
            WHERE `MaDC` = $id
            ;
        ";
        $result = mysql_query($query);
        if (mysql_num_rows($result) == 1) {
            $found = 1;
            while ($row = mysql_fetch_array($result)) {
                $diachi[] = $row;
            }
            $smarty->assign("diachi", $diachi);
        } else {
            header("Location: /diachi");
        }
    } else {
        //$action = null;
        header("Location: /diachi");
    }
}

if ($submit == "update") {
    $success = 0;

    $thanhpho = filter_input(INPUT_POST, "thanhpho");
    $quanhuyen = filter_input(INPUT_POST, "quanhuyen");
    $phuongxa = filter_input(INPUT_POST, "phuongxa");
    $address = filter_input(INPUT_POST, "address");
    $maDC = filter_input(INPUT_POST, "maDC");

    $query = "
        SELECT *
        FROM `diachi`
        WHERE `DiaChi`='$address' AND 
            `MaPX`=$phuongxa AND
            `MaQH`=$quanhuyen AND
            `MaTP`=$thanhpho AND
            `MaDC`!=$maDC
        ;
    ";
    $result = mysql_query($query);

    if (mysql_num_rows($result) != 0) {
        $success = -1;
    } else {
        $query = "
            UPDATE `diachi`
            SET `DiaChi`= '$address', 
                `MaPX` = $phuongxa,
                `MaQH` = $quanhuyen,
                `MaTP` = $thanhpho
            WHERE `MaDC` = $maDC
            ;
        ";
        $result = mysql_query($query);
        if ($result) {
            $success = 1;
        } else {
            $success = 0;
        }
    }
    $smarty->assign("success", $success);
}

if ($action == "delete") {
    $success = 0;

    $maDC = filter_input(INPUT_GET, "id");

    $query = "
        SELECT *
        FROM `diachi`
        WHERE `MaDC`=$maDC
        ;
    ";
    $result = mysql_query($query);

    if (mysql_num_rows($result) == 0) {
        $success = -1;
    } else {
        $query = "
            DELETE FROM `diachi`
            WHERE `MaDC` = $maDC
            ;
        ";
        $result = mysql_query($query);
        if ($result) {
            $success = 1;
        } else {
            $success = 0;
        }
    }
    $smarty->assign("success", $success);
}
$smarty->assign("action", $action);

$smarty->display("diachi.tpl");
